package com.yy.jhl.ui.user.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LoginInfo;
import com.yy.jhl.ui.main.activity.MainActivity;
import com.yy.jhl.ui.manager.activity.MainWithCodeActivity;
import com.yy.jhl.ui.user.contract.LoginWithCodeContract;
import com.yy.jhl.ui.user.presenter.LoginWithCodePresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/6.
 */

public class LoginWithCodeActivity extends BaseActivity<LoginWithCodePresenter> implements LoginWithCodeContract.View {
    @BindView(R.id.phone_edit)
    EditText phoneEdit;
    @BindView(R.id.code_edit)
    EditText codeEdit;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;

    @Override
    public int getLayoutId() {
        return R.layout.act_login_with_code;
    }

    @Override
    public void initView() {
        showTitle("邀请码登录", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        int loginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        if (loginType == 3){
            phoneEdit.setText(SPUtils.getSharedStringData(mContext,AppConstant.SP_KEY_USER_NAME));
            codeEdit.setText(SPUtils.getSharedStringData(mContext,AppConstant.SP_KEY_USER_PWD));
        }
    }

    @Override
    public void loginSuccess(LoginInfo loginInfo) {
        SPUtils.setSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE,3);
        SPUtils.setSharedStringData(mContext, AppConstant.SP_KEY_USER_NAME, phoneEdit.getText().toString());
        SPUtils.setSharedStringData(mContext, AppConstant.SP_KEY_USER_PWD, codeEdit.getText().toString());
        SPUtils.setSharedStringData(mContext, AppConstant.SP_KEY_TOKEN,loginInfo.getToken());
        SPUtils.setSharedObjectData(mContext,AppConstant.SP_KEY_USER_INFO,loginInfo.getUser());
        startActivity(MainWithCodeActivity.class);
        finish();
    }

    @Override
    public void loginFail() {

    }
    @OnClick({R.id.ensure_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ensure_btn:
                String phone = phoneEdit.getText().toString();
                String code = codeEdit.getText().toString();
                if (StringUtil.isEmpty(phone)){
                    showShortToast("手机号码不能为空");
                    return;
                }
                if (StringUtil.isEmpty(code)){
                    showShortToast("邀请码不能为空");
                    return;
                }
                mPresenter.login(phone,code);
                break;
        }
    }
}
